import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mvvm_provider/page/collection/widget/show_novels_item_widget.dart';
import 'package:mvvm_provider/page/home/data/model/cartoon_model.dart';
import 'package:mvvm_provider/routers/navigator_utils.dart';
import 'package:mvvm_provider/until/app_util.dart';

Future<CartoonModel?> showNovelWidget(
    BuildContext context, List<CartoonModel> dataList, List<CartoonModel> collectionList) async {
  return showModalBottomSheet(
    context: context,
    isScrollControlled: true,
    isDismissible: true,
    backgroundColor: Colors.transparent,
    builder: (BuildContext context) {
      return ShowNovelsWidget(
        dataList: dataList,
        collectionList: collectionList,
      );
    },
  );
}

class ShowNovelsWidget extends StatefulWidget {
  final List<CartoonModel> dataList;
  final List<CartoonModel> collectionList;
  const ShowNovelsWidget({super.key, required this.dataList, required this.collectionList});

  @override
  State<ShowNovelsWidget> createState() => _ShowNovelsWidgetState();
}

class _ShowNovelsWidgetState extends State<ShowNovelsWidget> {
  @override
  void initState() {
    super.initState();

    if (AppUtil.isNotEmpty(widget.collectionList)) {
      for (CartoonModel cartoonModel in widget.dataList) {
        int index =
            widget.collectionList.indexWhere((element) => element.itemId == cartoonModel.itemId);
        if (index >= 0) {
          cartoonModel.isCollection = true;
        }
      }
    }
  }

  /// 标题
  Widget _title() {
    TextStyle style = TextStyle(fontSize: 16.sp, color: Colors.black, fontWeight: FontWeight.w500);
    return Container(
      alignment: Alignment.center,
      height: 44.h,
      child: Text('小说', style: style),
    );
  }

  /// 关闭按钮
  Widget _clearWidget(BuildContext context) {
    return InkWell(
      onTap: () => NavigatorUtils.pop(context),
      child: Image.asset(
        'assets/images/ic_clean_all.png',
        width: 20.w,
        height: 20.w,
      ),
    );
  }

  /// 滚动列表
  Widget _listWidget() {
    return Expanded(
      child: GridView.builder(
          itemCount: widget.dataList.length,
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              childAspectRatio: 0.7, //宽高比
              crossAxisSpacing: 10.w, //水平间距
              mainAxisSpacing: 10.h, //垂直间距
              crossAxisCount: 2),
          padding: EdgeInsets.all(10.w),
          itemBuilder: (BuildContext context, int index) {
            return ShowNovelsItemWidget(
              index: index,
              model: widget.dataList[index],
              collectionTap: () => NavigatorUtils.pop(context, result: widget.dataList[index]),
            );
          }),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(12.r),
          topRight: Radius.circular(12.r),
        ),
      ),
      margin: EdgeInsets.only(
        top: ScreenUtil().statusBarHeight + 56.h,
      ),
      child: Column(
        children: [
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 12.w),
            child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [SizedBox(width: 1.w), _title(), _clearWidget(context)]),
          ),
          _listWidget(),
        ],
      ),
    );
  }
}
